import axios from "axios";
import qs from "qs";

/* 请求默认配置 */
export function httpGet(config) {
  const instance = axios.create({
    method: "get",
    baseURL: "http://localhost:2000",
    timeout: 5000,
    withCredentials: false,
  });

  instance.interceptors.request.use(
    (config) => {
      return config;
    },
    (err) => {
      console.log(err);
    }
  );

  instance.interceptors.response.use(
    (res) => {
      if (res.status == "200") {
        return Promise.resolve(res);
      } else {
        return Promise.reject(res);
      }
    },
    (err) => {
      return Promise.reject(err);
    }
  );

  return instance(config);
}

export function httpPost(config) {
  const instance = axios.create({
    method: "post",
    baseURL: "http://localhost:2000",
    timeout: 5000,
    withCredentials: false,
    headers: {
      "content-type": "application/x-www-form-urlencoded;charset=utf-8",
    },
  });

  instance.interceptors.request.use(
    (config) => {
      config.data = qs.stringify(config.data);
      return config;
    },
    (err) => {
      console.log(err);
    }
  );

  instance.interceptors.response.use(
    (res) => {
      if (res.status == "200") {
        return Promise.resolve(res);
      } else {
        return Promise.reject(res);
      }
    },
    (err) => {
      return Promise.reject(err);
    }
  );

  return instance(config);
}
